<?php

namespace App\Api\System;

use PhalApi\Api;

/**
 * 获取公司部门
 * @desc 获取公司部门
 * @author: dogstar 2018-01-09
 */
class RoleMenu extends Api
{
    public function getRules()
    {
        return array(
            'getList' => array(
                'page' => array('name' => 'page', 'type' => 'int', 'min' => 1, 'default' => 1, 'desc' => '页码'),
                'page_size' => array('name' => 'page_size', 'type' => 'int', 'default' => 10, 'desc' => '每页数量')
            ),
            'getAllList' => array(),
            'fixList' => array(),
            'getMenuList' => array(),
            'SaveIds' => array(),
            'getRoleMenuIds' => array(
                'id' => array('name' => 'id', 'type' => 'int', 'require' => true, 'desc' => '角色ID')
            )
        );
    }


    /**
     * 保存 saveIds
     * @desc 保存 saveIds
     * @return array {
     *   items: array 用户列表数据
     *   total: int 总记录数
     *   page: int 当前页码
     *   page_size: int 每页数量
     * }
     */
    public function SaveIds() {
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *,x-token,X-token');
        
        $input = json_decode(file_get_contents('php://input'), true);

        // return $input;
        $result = \PhalApi\DI()->notorm->phalapi_portal_admin_role
            ->where('id', $input['id'])
            ->update(  array( "permission" => json_encode($input['ids']) ) );

        \PhalApi\DI()->response->addResult('success', true);
        return $result;
    }

    /**
     * 获取菜单role-menu
     * @desc 获取role-menu
     * @return array {
     *   items: array 用户列表数据
     *   total: int 总记录数
     *   page: int 当前页码
     *   page_size: int 每页数量
     * }
     */
    public function getMenuList() {
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *,x-token,X-token');
    
        $items = \PhalApi\DI()->notorm->admin_menu
            ->select('parentId,id,menuType,title')
            ->fetchAll();

        \PhalApi\DI()->response->addResult('success', true);
        return $items;
    }



    /**
     * 获取角色菜单ID列表
     * @desc 根据角色ID获取菜单ID列表
     * @return array 菜单ID数组
     */
    public function getRoleMenuIds() {
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *,x-token,X-token');
        
        $id = $this->id;
        $role = \PhalApi\DI()->notorm->phalapi_portal_admin_role
            ->select('permission')
            ->where('id', $id)
            ->fetch();
            
        if (empty($role['permission'])) {
            return [];
        }
        
        \PhalApi\DI()->response->addResult('success', true);
        return json_decode($role['permission'], true);
    }

    /**
     * 获取角色列表
     * @desc 获取用户分页列表
     * @return array {
     *   items: array 用户列表数据
     *   total: int 总记录数
     *   page: int 当前页码
     *   page_size: int 每页数量
     * }
     */
    public function getList() {
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *,x-token,X-token');
        
        $page = $this->page;
        $pageSize = $this->page_size;
    
        $total = \PhalApi\DI()->notorm->phalapi_portal_admin_role->count();
        $items = \PhalApi\DI()->notorm->phalapi_portal_admin_role
            ->select('*')
            ->limit(($page - 1) * $pageSize, $pageSize)
            ->fetchAll();
        
        \PhalApi\DI()->response->addResult('success', true);
        return array(
            $items,
            'total' => $total,
            'currentPage' => $page,
            'pageSize' => $pageSize
        );
    }

    /**
     * 获取所有角色列表
     * @desc 获取用户分页列表
     * @return array {
     *   items: array 用户列表数据
     *   total: int 总记录数
     *   page: int 当前页码
     *   page_size: int 每页数量
     * }
     */
    public function getAllList() {
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *,x-token,X-token');
        
        $items = \PhalApi\DI()->notorm->phalapi_portal_admin_role
            ->select('*')
            ->fetchAll();
        
        \PhalApi\DI()->response->addResult('success', true);
        return $items;
    }

    /**
     * 更新公司部门
     * @desc 根据id更新公司部门
     * @return array 操作结果
     */
    public function fixList()
    {
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers:*');

        $input = json_decode(file_get_contents('php://input'), true);
        if (empty($input)) {
            \PhalApi\DI()->response->addResult('error', '更新数据不能为空');
            return ['error' => '更新数据不能为空'];
        }

        if (empty($input['name'])) {
            \PhalApi\DI()->response->addResult('error', 'name不能为空');
            return ['error' => 'name不能为空'];
        }
        $id = $input['id'];

        $result = \PhalApi\DI()->notorm->admin_dept
            ->where('id', $id)
            ->update($input);

        if ($result === false) {
            \PhalApi\DI()->response->addResult('error', '更新失败');
            return ['error' => '更新失败'];
        }

        \PhalApi\DI()->response->addResult('success', true);
        return ['success' => true];
    }

    
}